/*
 * @Author:Iyoole
 * @Time  :2017-12-07
 * @Description:选择插件
 * @git:
 * */

;!function(e){
    "use strict";
    var i,language={
            cn:{
                btn:['确定','取消'],
                title:'选择'
            },
            en:{
                btn:['Sure','Cancel'],
                title:'Select'
            }
        },c={
        isArr:function(o){
            return Object.prototype.toString.call(o)==='[object Array]'
        }
    };
    var select=function(tag){
        this.tag=i(tag);
        this.isPC=true;
        this.curIdx=[];
        this.curValue=[];
    };
    select.pt=select.prototype;
    select.pt.config={
        lang:'cn',//语言
        keyMap:{id:"id",value:"value",sub:'sub'}
    };
    select.pt.render=function(opts){
        var t=this;
        t.keyMap=opts.keyMap?opts.keyMap:t.config.keyMap;
        t.config.lang=opts.lang?opts.lang:t.config.lang;
        t.config.title=opts.title?opts.title:language[t.config.lang]['title'];
        t.view();
        t.json(opts.select);
        delete opts.select;
        t.device();
        t.listenerWheel();
    };
    select.pt.view=function(){
        var t=this;
        var $s=i('<div/>').addClass('JsSelect').append('<div class="selectGray"></div>').appendTo(i('body')),
            $m=i('<div/>').addClass('selectContent').appendTo($s),
            $b=i('<div/>').addClass('selectBtn').appendTo($m),
            $p=i('<div/>').addClass('selectPanel').appendTo($m),
            $w=i('<div/>').addClass('selectWheel').appendTo($p);
        var btnCancel=language[t.config["lang"]]["btn"][1],btnSure=language[t.config["lang"]]["btn"][0];
        i('<div/>').addClass('selectBd').append('<a href="javascript:;">'+btnCancel+'</a><span class="selectTitle">'+t.config.title+'</span><a href="javascript:;" class="selectSure">'+btnSure+'</a>').appendTo($b);
        $p.append('<div class="selectLine"></div><div class="selectMark"></div>');

        t.$Select=$s;
        t.$Wheel=$w;
    };
    select.pt.type1=function(){
        var t=this,h="";
        t.width();
        t.$Wheel.empty();
        for(var x=0;x<t.deepLive;x++){
            h+='<div class="selectItem" style="width:'+t.itemWidth+'%;"><ul data-wheel="'+x+'" style="transform:translate3d(0,80px,0)" data-scroll="80">';
            for(var y=0;y<t.data[x].length;y++){
                h+='<li>'+t.data[x][y]+'</li>'
            }
            t.curIdx[x]=0;
            t.curValue[x]=t.data[x][0];
            h+='</ul></div>';
        }
        t.$Wheel.append(h)
    };
    select.pt.type2=function(){
        var t=this,h="";
        t.width();
        t.$Wheel.empty();
        for(var x=0;x<t.deepLive;x++){
            h+='<div class="selectItem" style="width:'+t.itemWidth+'%;"><ul data-wheel="'+x+'" style="transform:translate3d(0,80px,0)" data-scroll="80">';
            for(var y=0;y<t.data[x].length;y++){
                h+='<li data-id="'+t.data[x][y][t.keyMap.id]+'">'+t.data[x][y][t.keyMap.value]+'</li>'
            }
            t.curIdx[x]=0;
            t.curId[x]=t.data[x][0][t.keyMap.id];
            t.curValue[x]=t.data[x][0][t.keyMap.value];
            h+='</ul></div>';
        }
        t.$Wheel.append(h)
    };
    select.pt.type3=function(){
        var t=this,h="";
        t.width();
        t.$Wheel.empty();
        for(var x=0;x<t.deepLive;x++){
            t.curIdx[x]=0;
            h+='<div class="selectItem" style="width:'+t.itemWidth+'%"><ul data-wheel="'+x+'" style="transform:translate3d(0,80px,0)"></ul></div>';
        }
        t.$Wheel.append(h);
        t.wheel(0);
    };
    select.pt.wheel=function(w){
        var t=this,k='sub';
        var ul=t.$Wheel.find('[data-wheel="'+w+'"]');
        ul.empty().css('transform','translate3d(0,80px,0)').attr('data-scroll',80);
        for(var m=0;m<w;m++){
            k+='-'+t.curIdx[m]
        }
        if(k in t.data&&t.data[k].length>0){
            var li='';
            for(var x=0;x<t.data[k].length;x++){
                li+='<li data-id="'+t.data[k][x][t.keyMap.id]+'">'+t.data[k][x][t.keyMap.value]+'</li>'
            }
            t.curId[w]=t.data[k][0][t.keyMap.id];
            t.curValue[w]=t.data[k][0][t.keyMap.value];
            ul.append(li);
        }else{
            ul.append('<li>--</li>');
            t.curValue[w]='';
            t.curId[w]='';
        }
        if(w<t.deepLive-1){
            w++;
            t.wheel(w)
        }
    };
    //检查是不是对象
    select.pt.json=function(d){
        var t=this;
        if(d){
            if(!c.isArr(d[0])){
                t.curId=[];
                t.FormatData(d);
            }else if(!c.isArr(d[0][0])&&(typeof d[0][0] ==='object')){
                t.curId=[];
                t.deepLive=d.length;
                t.data=d;
                t.type2();
            }
            else{
                t.deepLive=d.length;
                t.data=d;
                t.type1();
            }
        }else{
            t.data='Loading';
            t.$Wheel.append('<div class="selectTips">'+t.data+'</div>');
        }
    };
    //宽度计算
    select.pt.width=function(){
        var t=this;
        t.itemWidth=(100/t.deepLive).toFixed(2);
    };
    //格式化数据
    select.pt.FormatData=function(d){
        var t=this;
        t.data={};t.deepLive=1;t.isLinkage=1;
        t.data['sub']=[];
        i.each(d,function(m,n){
            var oj={};
            oj[t.keyMap.id]=n[t.keyMap.id];
            oj[t.keyMap.value]=n[t.keyMap.value];
            t.data['sub'].push(oj);
            t.deep(n,'sub-'+m);
        });
        t.type3();
    };
    select.pt.eachData=function(d,pm){
        var t=this;
        t.data[pm]=[];
        i.each(d,function(m,n){
            var oj={};
            oj[t.keyMap.id]=n[t.keyMap.id];
            oj[t.keyMap.value]=n[t.keyMap.value];
            t.data[pm].push(oj);
            t.deep(n,pm+'-'+m);
        });
        var ln = pm.split('-').length;
        t.deepLive=t.deepLive>ln?t.deepLive:ln;
    };
    //深度检测
    select.pt.deep=function(d,m){
        var t=this;
        if(t.keyMap.sub in d&&d[t.keyMap.sub].length>0){
            //有子类的话继续循环子类
            t.eachData(d[t.keyMap.sub],m);
        }
    };
    //判断是不是PC端
    select.pt.device=function(){
        var t=this;
        var sUserAgent = navigator.userAgent.toLowerCase();
        var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
        var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
        var bIsMidp = sUserAgent.match(/midp/i) == "midp";
        var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
        var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
        var bIsAndroid = sUserAgent.match(/android/i) == "android";
        var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
        var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
        if ((bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM)) {
            t.isPC = false;
        }
    };
    //滚动监听
    select.pt.listenerWheel=function(){
        var t=this,oy,cy,wy;
        t.$Wheel.find('ul').on('touchstart',function(ev){
            oy=ev.originalEvent.changedTouches[0].pageY;
        });
        t.$Wheel.find('ul').on('touchend',function(){
            var _t=i(this),ln=_t.find('li').length,idx;
            var mc=(wy%40)/40>0?(wy%40)/40>0.5?1:0:(wy%40)/40<-0.5?0:1;
            var nc=Math.floor(wy/40) + mc;
            if(wy>80){
                idx=0;
                _t.css('transform','translate3d(0,80px,0)').attr('data-scroll','80');
            }else if(wy<(3-ln)*40){
                idx=ln-1;
                _t.css('transform','translate3d(0,'+(3-ln)*40+'px,0)').attr('data-scroll',(3-ln)*40)
            }else{
                idx=2-nc;
                _t.css('transform','translate3d(0,'+nc*40+'px,0)').attr('data-scroll',nc*40)
            }
            t.current(idx,_t.data('wheel'));
        });
        t.$Wheel.find('ul').on('touchmove',function(ev){
            ev.preventDefault();
            var _t=i(this),sc=parseInt(_t.attr('data-scroll'));
            cy=ev.originalEvent.changedTouches[0].pageY - oy;
            wy=sc+cy;
            _t.css('transform','translate3d(0,'+wy+'px,0)');
        });
        if(t.isPC){
            t.$Wheel.on("selectstart",function() {
                return false
            });
            t.$Wheel.find('ul').on('mousedown',function(ev){
                oy=ev.pageY;
                var wh=i(this).attr('data-wheel');
                t['Status'+wh]=true;
            });
            t.$Wheel.find('ul').on('mousemove',function(ev){
                var _t=i(this),wh=_t.attr('data-wheel');
                ev.preventDefault();
                if(t['Status'+wh]){
                    var sc=parseInt(_t.attr('data-scroll'));
                    cy=ev.pageY - oy;
                    wy=sc+cy;
                    _t.css('transform','translate3d(0,'+wy+'px,0)');
                }
            });
            t.$Wheel.find('ul').on('mouseup',function(){
                var _t=i(this),wh=_t.data('wheel');
                if(t['Status'+wh]){
                    var ln=_t.find('li').length,idx;
                    t['Status'+wh]=false;
                    var mc=(wy%40)/40>0?(wy%40)/40>0.5?1:0:(wy%40)/40<-0.5?0:1;
                    var nc=Math.floor(wy/40) + mc;
                    if(wy>80){
                        idx=0;
                        _t.css('transform','translate3d(0,80px,0)').attr('data-scroll','80');
                    }else if(wy<(3-ln)*40){
                        idx=ln-1;
                        _t.css('transform','translate3d(0,'+(3-ln)*40+'px,0)').attr('data-scroll',(3-ln)*40)
                    }else{
                        idx=2-nc;
                        _t.css('transform','translate3d(0,'+nc*40+'px,0)').attr('data-scroll',nc*40)
                    }
                    t.current(idx,wh);
                }
            });
            t.$Wheel.find('ul').on('mouseleave',function(){
                var _t=i(this),wh=_t.data('wheel');
                if(t['Status'+wh]){
                    _t.mouseup();
                }
            });
        }
    };
    select.pt.current=function(idx,w){
        var t=this;
        var $li=t.$Wheel.find('[data-wheel="'+w+'"] li').eq(idx);
        t.curValue[w]=$li.text();
        ('curId' in t)?t.curId[w]=$li.attr('data-id'):'';
        t.curIdx[w]=idx;
        for(var y=w+1;y<t.deepLive;y++){
            t.curIdx[y]=0
        }
        t.isLinkage?t.wheel(w+1):'';
    };
    var JsSelect=function(tag){
        if(!tag) return console.error('JsSelect Say:No Trigger');
        return new select(tag)
    };

    JsSelect.v='1.0.1';
    if(window.fui && fui.define ){
        fui.define('jquery', function(exports){
            e.JsSelect=JsSelect;
            i=fui.$;
            exports('JsSelect', JsSelect);
        });
    }else{//如果直接引用的话
        e.JsSelect=JsSelect;
        i=e.jQuery;
    }
}(window);



/*
* 思路
* 1.先把数据进行格式化处理
*  a).单一非对象数据 --->处理
*  b).对象类数据    --->格式化处理
*
* */

